/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bussiness;

import Database.PostData;
import Entity.Post;
import Entity.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author cuong_000
 */
@WebServlet(name = "PostArt", urlPatterns = {"/PostArt"})
public class PostArt extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String status = request.getParameter("status");
        response.setContentType("text/html;charset=UTF-8");
        if (!status.isEmpty() && status.length() < 500 && status.length() > 0) {
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date date = new Date();
            User user = (User) request.getSession().getAttribute("user");
            Post post = setPost(status, user.getId());

            PrintWriter out = response.getWriter();
            try {
                int rs = PostData.insertPost(post);
                if (rs >= 1) {
                    out.println("<li>");
                    out.println("<div class=\"block block-inline\" style=\"width:690px;word-wrap: break-word;\">");
                    out.println("<span class=\"marker\"></span>");
                    out.println("<div class=\"caret\"></div>");
                    out.println("<div class=\"box-generic\">");
                    out.println("<div class=\"timeline-top-info border-bottom\">");
                    out.println("<i class=\"fa fa-user\"></i><b> " + user.getLname() + " " + user.getFname() + "</b>");
                    out.println("<br/><p style=\"padding : 1em\">" + status + "</p>");
                    out.println("<div class=\"timeline-bottom\">");
                    out.println("<i class=\"fa fa-clock-o\"></i> " + dateFormat.format(date));

                    out.println("</div>");
                    out.println("</div>");
                    
                    String holderId = "commentHolder" + rs;
                    out.println("<div id=\"" + holderId + "\">");
                    out.println("</div>");
                    
                    out.println("<div>");
                    out.println("<div class=\"input-group comment\">");
                    String cmtId = "cmt" + rs;
                    out.println("<input id=\"" + cmtId + "\" class=\"form-control\" placeholder=\"Comment here...\">  ");
                    out.println("<div class=\"input-group-btn\">");
                    out.println("<button class=\"btn btn-primary\" type=\"button\" onclick=\"SendComment(" + rs + ")\">Post</button>");
                    out.println("</div>");
                    out.println("</div>");
                    out.println("</div>");
                    out.println("</div>");
                    out.println("</div>");
                    out.println("</li>");
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PostArt.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(PostArt.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                out.close();
            }
        }
    }

    private Post setPost(String status, int userId) {
        Date date = new Date();
        Post post = new Post();
        post.setContent(status);
        post.setTimepost(new java.sql.Date(date.getTime()));
        post.setUsers_id(userId);
        return post;
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
